package com.hjb.controller;

import com.github.pagehelper.PageInfo;
import com.hjb.entity.User;
import com.hjb.service.UserService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author JianBinHuang
 * @Description
 * @Date 2021/9/9 19:00
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/showAll/{pageIndex}")
    public String showAll(@PathVariable String pageIndex, Model model){

        //查询书籍的分页信息
        PageInfo<User> pageInfo=userService.getUserPage(pageIndex);
        //存在域中
        model.addAttribute("pageInfo", pageInfo);
        //跳转到视图层
        return "show";
    }

    @RequestMapping("/updateUid/{id}")
    public String updateUid(@PathVariable int id,Model model){

        User user = userService.queryUserById(id);
        model.addAttribute("user", user);
        return "update";
    }

    @RequestMapping("/update")
    public String updateUser(User user){

        userService.updateUsers(user);
        return "redirect:/user/showAll/1";
    }

    @RequestMapping("/addUser")
    public String addUser(User user){

        userService.addUsers(user);
        return "redirect:/user/showAll/1";
    }

    @RequestMapping("/deleteById/{id}")
    public String deleteById(@PathVariable int id){
        userService.deleteById(id);
        return "redirect:/user/showAll/1";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){

        return "redirect:/add.jsp";
    }
}
